﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;

using Nebular.ExpenseManagement.Infrastructure;

namespace Nebular.ExpenseManagement.Model
{
    [Serializable]
    public class Account : MarshalByRefObject, IAccount
    {
        readonly ExpContainer _context;
        readonly MembershipProvider _membershipProvider;
        readonly string _tenant;
        readonly string _username;
        readonly Guid _id;
        readonly IEnumerable<IUser> _users;
        readonly IEnumerable<IGroup> _groups;
        readonly IPrincipal _superUser;

        public Account(
            ExpContainer context,
            ITenantProvider tenant,
            MembershipProvider membershipProvider
            )
        {
            if (context == null)
                throw new ArgumentNullException("context");
            if (tenant == null)
                throw new ArgumentNullException("tenant");
            if (membershipProvider == null)
                throw new ArgumentNullException("membershipProvider");
            _context = context;
            var account = (from a in _context.xpAccounts
                           where a.Name == tenant.Tenant
                           select a).First();
            _users = User.Convert(account.Principals.Where(p => p.PrincipalType == "u"));
            _groups = Group.Convert(account.Principals.Where(p => p.PrincipalType == "g"));
            if (account.SuperUser != null)
                _superUser = Principal.FromExpPrincipal(account.SuperUser);
            _tenant = tenant.Tenant;
            _username = tenant.Username;
            _membershipProvider = membershipProvider;
        }

        #region IAccount Members

        public IEnumerable<IUser> Users
        {
            get {
                return _users;
            }
        }

        public IEnumerable<IGroup> Groups
        {
            get
            {
                return _groups;
            }
        }

        public string Name
        {
            get { return _tenant; }
        }

        public IUser Me
        {
            get
            {
                return Users.First(u => u.Name == _username);
            }
        }

        public Guid Id
        {
            get { return _id; }
        }

        public IEnumerable<IDimension> Dimensions
        {
            get { throw new NotImplementedException(); }
        }

        public IPrincipal SuperUser
        {
            get
            {
                return _superUser;
            }
        }

        public IAccount CreateUser(IUser newUser)
        {
            throw new NotImplementedException();
        }

        public IAccount CreateGroup(IGroup newGroup)
        {
            throw new NotImplementedException();
        }

        public IAccount CreateDimension(IDimension dimension)
        {
            throw new NotImplementedException();
        }

        public IAccount CreateTransactions(IEnumerable<ITransaction> newTransactions)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<ITransaction> OrphanedTransactions
        {
            get { throw new NotImplementedException(); }
        }

        public IAccount SetSuperUser(IPrincipal superUser)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<IApprovedTransaction> ApprovedTransactions
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion

        #region IAccount Members


        public IAccount CreateUser(string name, string email, IPrincipal approveBy, IEnumerable<string> creditCards)
        {
            throw new NotImplementedException();
        }

        public IAccount UpdateUser(Guid id, string name, string email, IPrincipal approveBy, IEnumerable<string> creditCards)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
